<template>
  <div class="home">
    <!-- 
      <el-container>：外层容器。当子元素中包含 <el-header> 或 <el-footer> 时，全部子元素会垂直上下排列，否则会水平左右排列。
      <el-header>：顶栏容器。
      <el-aside>：侧边栏容器。
      <el-main>：主要区域容器。
      <el-footer>：底栏容器。
    -->
    <el-container>
      <el-header height="80px">
        <div class="logo">
          <img src="../assets/logo.png" />
        </div>
        <el-radio-group v-model="isCollapse">
          <span class="menu" @click="isCollapse = !isCollapse">
            <i class="el-icon-menu"></i>
          </span>
        </el-radio-group>
        <span class="title">黑马头条后台管理系统</span>
        <img :size="50" class="avatar" :src="userinfo.head_img | imgUrl" />
        <el-dropdown trigger="click">
          <span class="nickname el-dropdown-link"
            >欢迎你：{{ userinfo.nickname }}
            <i class="el-icon-arrow-down"></i>
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item :style="{ borderBottom: '1px solid #333' }"
              >首页</el-dropdown-item
            >
            <el-dropdown-item>退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-header>
      <el-container>
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <!-- 
          组件解读：
            <el-menu>           整个菜单
            <el-submenu>        一级导航菜单编组(带展开功能)
              <template slot="title">  标题插槽
            <el-menu-item>     一级菜单子项/二级菜单子项(没有展开功能)
          属性解读：
            index 属性         🔔注意：设置成不重复的字符串
            default-active     默认选中项，配合 index 属性实现默认选中效果
        -->
          <el-menu
            class="el-menu-vertical-demo"
            :collapse="isCollapse"
            background-color="#333"
            text-color="#fff"
            active-text-color="#ffd04b"
            :collapse-transition="false"
            :unique-opened="true"
            router
          >
            <!-- 首页 -->
            <el-menu-item index="/home">
              <i class="el-icon-s-home"></i>
              <span slot="title">首页</span>
            </el-menu-item>
            <!-- 文章管理 -->
            <el-submenu>
              <template slot="title">
                <i class="el-icon-document"></i>
                <span slot="title">文章管理</span>
              </template>
              <el-menu-item index="/home/list">文章列表</el-menu-item>
              <el-menu-item index="/home/publish">文章添加</el-menu-item>
            </el-submenu>
            <!-- 栏目管理 -->
            <el-submenu>
              <template slot="title">
                <i class="el-icon-circle-plus"></i>
                <span slot="title">栏目管理</span>
              </template>
              <el-menu-item index="1-1">栏目列表</el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>

        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { userDetailByIdAPI } from "../apis/user";
import { getUserId } from "../utils/localData";

export default {
  data() {
    return {
      userinfo: {},
      isCollapse: true,
    };
  },

  async created() {
    // 获取用户 id，发送请求的时候需要用到
    const id = getUserId();
    // console.log(id);
    // 获取用户信息
    const res = await userDetailByIdAPI(id);
    // console.log(res);
    // 把用户信息绑定到页面中
    this.userinfo = res.data.data;
  },
};
</script>

<style lang="less" scoped>
.el-container {
  height: 100vh;
}
.el-header {
  background-color: #333;
  color: #fff;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding: 0 20px 0 0;
  .logo {
    width: 200px;
    background-color: #fff;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 80px;
  }
  .menu {
    font-size: 30px;
    margin-left: 20px;
  }
  .el-dropdown-link {
    cursor: pointer;
    color: white;
    .nickname {
      margin: 0 10px;
      > i {
        font-size: 20px;
      }
    }
  }
  .title {
    font-size: 20px;
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  .avatar {
    width: 50px;
    height: 50px;
    border-radius: 50%;
  }
}
.el-aside {
  background-color: #333;
  color: #fff;
  .el-menu-vertical-demo:not(.el-menu--collapse) {
    width: 200px;
    min-height: 400px;
  }
}
.el-main {
  background-color: #e9eef3;
  color: #333;
}
</style>
